﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SO_DataFetcher.PubFunctions.DataBaseHelp
{
    public class DataBaseHelper:IDataHelper
    {
        public virtual void Instance()
        {
            throw new NotImplementedException();
        }

        public virtual void GetConnectString(string ip, string port, string name, string psw, string dataTable)
        {
            throw new NotImplementedException();
        }

        public virtual void ExecuteSql(string sql)
        {
            throw new NotImplementedException();
        }

        public virtual DataSet GetDataSet(string sql)
        {
            throw new NotImplementedException();
        }

        public virtual void OpenConnection()
        {
            throw new NotImplementedException();
        }

        public virtual void CloseConnection()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 初始化Oracle数据库连接
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <param name="name"></param>
        /// <param name="psw"></param>
        /// <param name="dataTable"></param>
        public void Init(string ip, string port, string name, string psw, string dataTable)
        {
            if (string.IsNullOrEmpty(ip))
            {
                MessageBox.Show(@"IP地址为空");
                return;
            }

            if (string.IsNullOrEmpty(port))
            {
                MessageBox.Show(@"端口为空");
                return;
            }

            if (String.IsNullOrEmpty(name))
            {
                MessageBox.Show(@"用户名为空");
                return;
            }

            if (string.IsNullOrEmpty(psw))
            {
                MessageBox.Show(@"密码为空");
                return;
            }

            if (string.IsNullOrEmpty(dataTable))
            {
                MessageBox.Show(@"数据库名为空");
                return;
            }

            GetConnectString(ip, port, name, psw, dataTable);
        }

        /// <summary>
        /// 处理流程
        /// </summary>
        /// <param name="sqls"></param>
        /// <returns></returns>
        public DataTable ExecuteSqls(IEnumerable<string> sqls)
        {
            DataTable dataTable = new DataTable();

            foreach (string sql in sqls)
            {
                try
                {

                    dataTable = GetDataTable(sql);
                }
                catch (Exception exception)
                {
                    Console.WriteLine(exception);
                }
            }

            return dataTable;
        }

        public DataTable GetDataTable(string sql)
        {
            OpenConnection();

            return GetDataSet(sql).Tables[0];
        }

        /// <summary>
        /// 数据库连接字符串
        /// </summary>
        public string Connecting { get; set; }


    }
}
